package com.wly
{
	import com.wly.datapool.bullet.BulletInfo;
	import com.wly.datapool.monster.MonsterInfo;
	import com.wly.datapool.tower.TowerInfo;
	import com.wly.dynamicc.DynamicManager;
	import com.wly.dynamicc.DynamicObject;
	
	import flash.display.MovieClip;

	public class ActiveManager
	{
		private var _dynamicObj:DynamicObject;
		private var _activeMonsters:Object;
		private var _activeBullets:Object;
		private var _activeTowers:Object;
		private var _activeLayer:MovieClip;
		
		static private var MONSTERKEY:int;
		static private var BULLETKEY:int;
		static private var TOWERKEY:int;
		
		static private var _instance:ActiveManager;
		
		public function ActiveManager()
		{
			_instance = this;
			_activeLayer = new MovieClip;
			_activeMonsters = new Object;
			_activeBullets  = new Object;
			_activeTowers   = new Object;
			MONSTERKEY = -1;
			BULLETKEY  = -1;
			TOWERKEY   = -1;
			
			_dynamicObj = new DynamicObject;
			_dynamicObj.action = onTick;
			DynamicManager.getInstance().registerObj(_dynamicObj);
		}
		
		static public function getInstance():ActiveManager
		{
			return _instance;
		}
		
		public function get activeLayer():MovieClip
		{
			return _activeLayer;
		}
		
		public function onTick():void
		{
			//trace(this, "do hittest here");
			var tower:TowerInfo;
			var monster:MonsterInfo;
			var bullet:BulletInfo;
			
			for each(tower in _activeTowers)
			{
				if(!tower.hasMonster)
				{
					for each(monster in _activeMonsters)
					{
						var disX:Number = Math.abs(monster.centerX-tower.model.x);
						var disY:Number = Math.abs(monster.centerY-tower.model.y);
						var dis:int = int(Math.sqrt(disX*disX+disY*disY));
						if(dis<=tower.attackArea)
						{
							tower.targetMonster = monster;
							break;
						}
					}
				}
			}
			
			for each(bullet in _activeBullets)
			{
				if(bullet.model.hitTestObject(bullet.targetMonster.model))
				{
					bullet.targetMonster.hp -= bullet.attack;
					bullet.destory();
				}
			}
		}
		
		public function addMonster(monster:MonsterInfo):void
		{
			_activeMonsters[++MONSTERKEY] = monster;
			monster.activeKey = MONSTERKEY;
			_activeLayer.addChild(monster.model);
		}
		
		public function removeMonster(monster:MonsterInfo):void
		{
			if(monster.model.parent == _activeLayer)
			{
				_activeLayer.removeChild(monster.model);
			}
			delete _activeMonsters[monster.activeKey];
		}
		
		public function addBullet(bullet:BulletInfo):void
		{
			_activeBullets[++BULLETKEY] = bullet;
			bullet.activeKey = BULLETKEY;
			_activeLayer.addChild(bullet.model);
		}
		
		public function removeBullet(bullet:BulletInfo):void
		{
			_activeLayer.removeChild(bullet.model);
			delete _activeBullets[bullet.activeKey];;
		}
		
		public function addTower(tower:TowerInfo):void
		{
			_activeTowers[++TOWERKEY] = tower;
			tower.activeKey = TOWERKEY;		
			_activeLayer.addChildAt(tower.model, 0);
		}
		
		public function removeTower(tower:TowerInfo):void
		{
			_activeLayer.removeChild(tower.model);
			delete _activeTowers[tower.activeKey];
		}
	}
}